<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>添加新闻</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/main.css">
    <style>
        .layui-form-select dl {
            z-index: 2;
        }

        #thumb_list {
            padding-top: 8px;
        }

        #thumb_list img {
            border: 1px solid #DDD;
            padding: 3px;
            border-radius: 5px;
        }

        #thumb_list span {
            position: relative;
            display: inline-block;
            margin-right: 5px;
        }

        #thumb_list span .delimg {
            position: absolute;
            right: 3px;
            top: 3px;
        }
    </style>
    <script src="layui/layui.js"></script>
    <script src="js/jquery-1.9.1.min.js"></script>
</head>
<body>
<div class="layui-fluid">
    <form class="layui-form" action="" method="post">
        <div class="layui-form-item" style="margin-top: 10px">
            <label class="layui-form-label">新闻分类：</label>
            <div class="layui-input-inline">
                <select name="cid" required lay-verify="required" lay-filter="lanmu">
                    <option value="">请选择新闻分类</option>
                    <?php foreach ($cateArr as $v) {?>
                    <option value="<?= $v['id'] ?>"><?= trim($v['cName']) ?></option>
                    <?php } ?>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新闻标题：</label>
            <div class="layui-input-block">
                <input type="text" name="title" value="" required lay-verify="required" placeholder="请填写新闻标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">关键词：</label>
            <div class="layui-input-block">
                <input type="text" name="keyword" value="" placeholder="请填写关键词"  class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述：</label>
            <div class="layui-input-block">
                <textarea name="desc" placeholder="请填写描述" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">摘要：</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请填写摘要" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章作者：</label>
            <div class="layui-input-inline">
                <input type="text" name="author" value=" <?= $_SESSION['userID'] ?>" placeholder="请填写文章作者" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">添加时间：</label>
            <div class="layui-input-inline">
                <input type="text" name="addtime" value="<?= date('Y-m-d H:i:s', time()) ?>" required  lay-verify="required" class="layui-input" id="addTime">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">缩略图</label>
            <div class="layui-input-block">
                <input type="hidden" name="newsPicUrl" class="layui-input">
                <button type="button" class="layui-btn" id="uploadimg">
                    <i class="layui-icon layui-icon-upload"></i>上传缩略图
                </button>
                <div id="thumb_list"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文章内容</label>
            <div class="layui-input-block">
                <textarea id="content" name="content" type="text/plain" style="height:300px;"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">添加文章</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="ueditor/ueditor.all.js"></script>
<script>
    var ue = UE.getEditor('content');
    layui.use(['form', 'upload', 'laydate'], function () {
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#addTime' //指定元素
            , type: 'datetime'
        });
        //监听提交
        form.on('submit(formDemo)', function (data) {
        });
        //创建一个上传组件
        upload.render({
            elem: '#uploadimg'
            , url: 'upload.php'
            , accept: 'images' //允许上传的文件类型
            , field: "imgfile"
            , size: 2048 //最大允许上传的文件大小
            , done: function (res, index, upload) { //上传后的回调
                layer.close(layer.index, {isOutAnim: true});
                setTimeout(function () {
                    layer.msg(res.msg);
                    if (res.code == 1) {
                        var newsPicUrl = $('input[name=newsPicUrl]').val();
                        if (newsPicUrl == "") {
                            $('input[name=newsPicUrl]').val(res.img);
                        } else {
                            $('input[name=newsPicUrl]').val(newsPicUrl + "," + res.img);
                        }
                        var str;
                        str = "<span>";
                        str = str + '<img src="' + res.img + '" alt="" height="100" width="100">';
                        str = str + '<button type="button" class="layui-btn layui-btn-danger layui-btn-mini delimg" onclick="delimg(this);" data="' + res.img + '">';
                        str = str + '<i class="layui-icon layui-icon-delete"></i>';
                        str = str + '</button>';
                        str = str + '</span>';
                        $('#thumb_list').append(str);
                    }
                }, 500);
            }
            , before: function () {
                var index = layer.load();
            }
        })
    });
    //删除内容缩略图片
    function delimg(obj) {
        var delUrl = $(obj).attr('data');
        $.ajax({
            type: "post",
            url: "imgDel.php",
            data: {'url': delUrl},
            dataType: "json",
            success: function (res) {
                console.log(res.code);
                var newsPicUrl = $('input[name=newsPicUrl]').val();
                var str = "";
                if (res.code == 1) {
                    //栏目图片文本框中图片地址处理
                    if (newsPicUrl == delUrl) {
                        $('input[name=newsPicUrl]').val('');
                    } else {
                        str = newsPicUrl.replace(delUrl + ",", "");
                        str = str.replace("," + delUrl, "");
                        $('input[name=newsPicUrl]').val(str);
                    }
                    //删除/移除缩略图
                    $(obj).parent().remove();
                    layer.msg(res.msg);
                }
                if (res.code == 0) {
                    layer.msg(res.msg);
                }
            }
        });
    }
    $(function () {
        $('#righttitle', window.parent.document).text($('title').text());
    });
</script>
</body>
</html>


